import { config } from '../config.js'

// # 解构
class HTTP {
    _request(url, resolve, reject, data = {}, method = 'GET') {
        wx.request({
            url: config.api_base_url + url,
            method: method,
            data: data,
            header: {
                'content-type': method.toLowerCase() === 'get' ? 'application/json' : 'application/x-www-form-urlencoded',
                'accessToken': wx.getStorageSync('accessToken')
            },
            success: (res) => {
                resolve(res.data)
            },
            fail: (err) => {
                reject()
                this._show_error(1)
            }
        })
    }
    _show_error(error_code) {
        if (!error_code) {
            error_code = 1
        }
        wx.showToast({
            title: '抱歉，出现了一个错误',
            icon: 'none',
            duration: 2000
        })
    }
}

export { HTTP }